package com.fancyy.scm.activity;

import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.View;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.adapter.BRBaseAdapter;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.bean.ReportDataPok;
import com.fancyy.scm.bean.ReportDataTracking;
import com.fancyy.scm.databinding.DishisiyeBinding;
import com.fancyy.scm.util.DateUtil;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.fancyy.scm.BRConstant.REPORT_POY;
import static com.fancyy.scm.BRConstant.REPORT_TRCAKING;

@Translucent
@Layout(R.layout.dishisiye)
public class TrackReportActivity extends BaseActivity<DishisiyeBinding> {
    BRBaseAdapter adapter = new BRBaseAdapter();
    List<Object> list = new ArrayList<>();
    public int type; // 1-track 2-pok
    public ObservableField<String> poNo = new ObservableField<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 1);
        super.onCreate(savedInstanceState);
        if (type == 1) {
            getDataBinding().title.setText("Tracking Report");
            adapter.init(this, R.layout.item_report_tracking, list);
			getDataBinding().llPoNo.setVisibility(View.VISIBLE);
            getDataBinding().llPoNoEdit.setVisibility(View.VISIBLE);        
		} else {
            getDataBinding().title.setText("Poka-Yoke Report");
            adapter.init(this, R.layout.item_report_pok, list);
        }
        getDataBinding().listView.setAdapter(adapter);
        getDataBinding().begin.setText(DateUtil.getToday());
        getDataBinding().end.setText(DateUtil.getToday());
    }


    public void onReportSearchClick(View view) {
        if (type == 1) {
            loadDataTracking();
        } else {
            loadDataPok();
        }
    }

    private void loadDataTracking() {
        String url = REPORT_TRCAKING;// + "?StartDate=" + pageNo + "&EndDate=" + pageSize;
        BRDataTransport.create(url)
                .addParam("PoNO", poNo.get())
                .addParam("StartDate", getDataBinding().begin.getText().toString())
                .addParam("EndDate", getDataBinding().end.getText().toString())
                .addParam("Operator", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener<List<ReportDataTracking>>() {
                    @Override
                    public void onSuccess(List<ReportDataTracking> data) {
                        list.clear();
                        if (data != null && data.size() > 0) {
                            list.addAll(data);
                        } else {
                            showToast("No data be found");
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, new TypeToken<List<ReportDataTracking>>(){});
    }

    private void loadDataPok() {
        String url = REPORT_POY;// + "?StartDate=" + pageNo + "&EndDate=" + pageSize;
        BRDataTransport.create(url)
                .addParam("StartDate", getDataBinding().begin.getText().toString())
                .addParam("EndDate", getDataBinding().end.getText().toString())
                .addParam("Operator", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener<List<ReportDataPok>>() {
                    @Override
                    public void onSuccess(List<ReportDataPok> data) {
                        list.clear();
                        if (data != null && data.size() > 0) {
                            list.addAll(data);
                        } else {
                            showToast("No data be found");
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, new TypeToken<List<ReportDataPok>>(){});
    }

    public void onEndDateClick(View view) {
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.toDate(getDataBinding().begin.getText().toString());
        calendar.setTime(date);
        DateUtil.showDatePickerDialog(this, getDataBinding().end, calendar);
    }

    public void onBeginDateClick(View view) {
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.toDate(getDataBinding().begin.getText().toString());
        calendar.setTime(date);
        DateUtil.showDatePickerDialog(this, getDataBinding().begin, calendar);
    }
}
